﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.CCFlow.EV;
using Microsoft.Web.UI.WebControls;
using BEGDC.Utility.ExceptionManagement;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.UserManagement;

namespace BEGDC.CC.Web.UserControls.EV
{
    /// <summary>
    /// Summary description for ActionPoint1.
    /// </summary>
    public class ActionPoint : System.Web.UI.Page, UserControls.IPageAccess
    {
        #region Controls
        protected System.Web.UI.WebControls.DropDownList ddlType;
        protected System.Web.UI.WebControls.TextBox txtNo;
        protected System.Web.UI.WebControls.DropDownList ddlPRT;
        protected System.Web.UI.WebControls.DropDownList ddlClassfication;
        protected System.Web.UI.WebControls.DropDownList ddlCAT;
        protected System.Web.UI.WebControls.TextBox txtDueTime;
        protected System.Web.UI.WebControls.Button ibtnSubmit;
        protected System.Web.UI.WebControls.Button ibtnSave;
        protected System.Web.UI.WebControls.TextBox txtDescription;
        protected System.Web.UI.WebControls.TextBox txtOperator;
        protected System.Web.UI.WebControls.Label lblTypeValue;
        protected System.Web.UI.WebControls.Label lblAPNoValue;
        protected System.Web.UI.WebControls.Label lblStatusValue;
        protected System.Web.UI.WebControls.Label lblPRTValue;
        protected System.Web.UI.WebControls.Label lblClassValue;
        protected System.Web.UI.WebControls.Label lblCATValue;
        protected System.Web.UI.WebControls.Label lblDueTimeValue;
        protected System.Web.UI.WebControls.Label lblOperatorValue;
        protected System.Web.UI.HtmlControls.HtmlTableCell Description;
        protected System.Web.UI.WebControls.DataGrid dtGridAPComment;
        protected System.Web.UI.HtmlControls.HtmlTableRow attachment;
        protected System.Web.UI.HtmlControls.HtmlTableRow APTrace;
        protected System.Web.UI.WebControls.DataGrid dtGridAPContributers;
        protected System.Web.UI.WebControls.Button imgAddAPComment;
        protected System.Web.UI.WebControls.Button imgAPOwner;
        protected System.Web.UI.WebControls.Button imgAPContributer;
        protected System.Web.UI.WebControls.DataGrid dtGridAPOwner;
        protected System.Web.UI.HtmlControls.HtmlTableCell Td1;
        protected System.Web.UI.WebControls.TextBox txtStatus;
        protected BEGDC.CC.Web.UserControls.EV.APAttachment apAttachment1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl spanClosedDate;
        protected System.Web.UI.WebControls.TextBox txtClosedDate;
        protected System.Web.UI.WebControls.Label lbClosedDate;
        protected System.Web.UI.HtmlControls.HtmlGenericControl spanDueDate;
        protected System.Web.UI.HtmlControls.HtmlTableRow tr_ClosedDate;
        protected System.Web.UI.WebControls.Button ibt_Modify;

        private UserControls.AccessControl apAccess;

        #endregion

        #region Properties


        private bool IsReadOnly
        {
            get
            {
                if (ViewState["IsReadOnly"] == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["IsReadOnly"]);
                }
            }
            set
            {
                ViewState["IsReadOnly"] = value;
            }
        }


        protected int MeetingID
        {
            get
            {
                if (Request.QueryString["MeetingID"] == null)
                    return Convert.ToInt32(ViewState["MeetingID"]);
                else
                    return Convert.ToInt32(Request.QueryString["MeetingID"]);
            }
            set
            {
                ViewState["MeetingID"] = value;
            }
        }

        protected int APID
        {
            get
            {
                if (Request.QueryString["APID"] == null)
                {
                    if (ViewState["APID"] != null)
                    {
                        return Convert.ToInt32(ViewState["APID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return Convert.ToInt32(Request.QueryString["APID"]);
                }
            }
            set
            {
                ViewState["APID"] = value;
            }
        }

        private bool IsHasOwner
        {
            get
            {
                if (ViewState["HasOwner"] == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            set
            {
                ViewState["HasOwner"] = value;
            }
        }


        private Entity.ActionPoint ActionPointRecord
        {
            get
            {
                if (Session[MeetingID + APID + "ActionPoint"] == null)
                    return null;
                else
                    return (Entity.ActionPoint)Session[MeetingID + APID + "ActionPoint"];
            }
            set
            {
                Session[MeetingID + APID + "ActionPoint"] = value;
            }
        }

        private object APOwnerContributer
        {
            get
            {
                return Session["APOwnerContributer"];
            }
            set
            {
                Session["APOwnerContributer"] = value;
            }
        }

        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here

            if (!IsPostBack)
            {
                ActionPointRecord = null;
                APOwnerContributer = null;

                this.Access = new APAccessControl(this.APID);
                Access.State = Request.QueryString["Status"];

                InitAPInfo();


                this.ibtnSubmit.Attributes["onclick"] = "javascript:OnSubmit();";
                this.ibtnSave.Attributes["onclick"] = "javascript:OnSave();";
                this.imgAPOwner.Attributes["onclick"] = "javascript:OpenAPUserForm('Owner')";
                this.imgAPContributer.Attributes["onclick"] = "javascript:OpenAPUserForm('Contributer')";
                this.imgAddAPComment.Attributes["onclick"] = "javascript:AddAPComment();";
            }

            InitOwnerContributer();
            InitCommentHistory();

            if (this.Request.QueryString["Status"] == "Readonly")
            {
                this.ibtnSubmit.Visible = false;
                this.ibtnSave.Visible = false;
            }

            //Ask for时，要求提交人可添加、删除APContributers 
            if (Request.QueryString["State"] == "Query")
            {
                this.dtGridAPContributers.Columns[4].Visible = true;
                this.imgAPContributer.Visible = true;
                this.ibtnSave.Visible = true;
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ibtnSubmit.Click += new EventHandler(ibtnSubmit_Click);
            this.ibtnSave.Click += new EventHandler(ibtnSave_Click);
            this.dtGridAPOwner.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtGridAPOwner_ItemCommand);
            this.dtGridAPOwner.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dtGridAPOwner_ItemDataBound);
            this.dtGridAPContributers.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtGridAPContributers_ItemCommand);
            this.dtGridAPContributers.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dtGridAPContributers_ItemDataBound);
            this.ibt_Modify.Click += new EventHandler(ibt_Modify_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void InitOwnerContributer()
        {
            DataTable OwnerContributerTable = null;
            if (this.APOwnerContributer != null)
            {
                OwnerContributerTable = (DataTable)APOwnerContributer;
            }
            else
            {
                if (this.APID > 0)
                {
                    IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                    OwnerContributerTable = queryService.GetAssignedDeptHeaders(APID, string.Empty);

                }
                if (OwnerContributerTable == null || OwnerContributerTable.Rows.Count == 0)
                {
                    OwnerContributerTable = new DataTable();
                    OwnerContributerTable.Columns.Add(new DataColumn("RowId", typeof(System.Int32)));
                    OwnerContributerTable.Columns.Add(new DataColumn("APPartnerID", typeof(System.Int32)));
                    OwnerContributerTable.Columns.Add(new DataColumn("EmployeeNo", typeof(System.String)));
                    OwnerContributerTable.Columns.Add(new DataColumn("Description", typeof(System.String)));
                    OwnerContributerTable.Columns.Add(new DataColumn("APType", typeof(System.String)));
                    OwnerContributerTable.Columns.Add(new DataColumn("NameEN", typeof(System.String)));
                }
                APOwnerContributer = SetRowID(OwnerContributerTable);
            }
            OwnerContributerTable.DefaultView.RowFilter = "APType = 'Owner'";
            this.dtGridAPOwner.DataSource = OwnerContributerTable.DefaultView;
            this.dtGridAPOwner.DataBind();

            OwnerContributerTable.DefaultView.RowFilter = "APType = 'Contributer'";
            this.dtGridAPContributers.DataSource = OwnerContributerTable.DefaultView;
            this.dtGridAPContributers.DataBind();

            if (this.IsReadOnly || (this.imgAPOwner.Visible == false && this.imgAPContributer.Visible == false))
            {

                this.dtGridAPOwner.Columns[4].Visible = false;
                this.dtGridAPContributers.Columns[4].Visible = false;
            }
        }

        private DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1][0] = i.ToString();
                }
            return table;
        }
        /// <summary>
        /// 
        /// </summary>
        private void InitDropDownList()
        {
            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;

            ddlType.DataSource = queryService.GetDicItemByTypeName(BEGDC.CC.BL.Common.DicType.APTYPE);
            ddlType.DataTextField = "DicItemKey";
            ddlType.DataBind();

            ddlPRT.DataSource = queryService.GetDicItemByTypeName(BEGDC.CC.BL.Common.DicType.APPRT);
            ddlPRT.DataTextField = "DicItemKey";
            ddlPRT.DataBind();

            this.txtStatus.Text = "Open";
            //			ddlStatus.DataSource = queryService.GetDicItemByTypeName( BEGDC.CC.BL.Common.DicType.APSTATUS );
            //			ddlStatus.DataTextField = "DicItemKey";
            //			ddlStatus.DataBind();

            ddlClassfication.DataSource = queryService.GetDicItemByTypeName(BEGDC.CC.BL.Common.DicType.CLASS);
            ddlClassfication.DataTextField = "DicItemKey";
            ddlClassfication.DataBind();

            ddlCAT.DataSource = queryService.GetDeptByTypeName(BEGDC.CC.BL.Common.DicType.CAT);
            ddlCAT.DataTextField = "DepartmentCode";
            ddlCAT.DataBind();

        }


        /// <summary>
        /// 
        /// </summary>



        private void InitCommentHistory()
        {
            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
            DataTable commentsTable = evService.GetAPCommentsHistory(this.APID);

            dtGridAPComment.DataSource = commentsTable.DefaultView;
            dtGridAPComment.DataBind();

        }

        private void InitAPInfo()
        {
            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;

            // New Action Point
            if (this.APID == 0)
            {
                if (this.txtNo.Text == "")
                    this.txtNo.Text = evService.GenerateAPNo(this.MeetingID);

                this.ibtnSubmit.Visible = false;

                //set init data for dropdownlist and listboxes
                InitDropDownList();

                //no ap comments
                this.APTrace.Visible = false;
                //no attachments are allowed
                this.attachment.Visible = false;

            }
            else
            {
                evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                this.ActionPointRecord = evService.GetAPByID(this.APID);

                DataTable APInfo = evService.GetAPInfo(this.APID);

                #region Set AP Status
                if (APInfo != null && APInfo.Rows.Count > 0)
                {
                    //AP applicant fill in AP Informations
                    if (APInfo.Rows[0]["APStep"].ToString() == WorkTransition.AP_Request)
                    {
                        InitDropDownList();
                    }
                    else
                    {

                        BEGDC.CC.BL.UserManagement.IUser userService = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
                        string access = userService.GetAPAccessAuthority(HttpContext.Current.User.Identity.Name, this.APID);

                        //AP Content are not allowed to be changed
                        this.IsReadOnly = true;


                        #region Set security info according to AP step and the user's security level

                        bool isAdmin = userService.IsBusinessAdmin(HttpContext.Current.User.Identity.Name);
                        if (!isAdmin)
                        {
                            this.imgAddAPComment.Visible = true;
                            this.imgAPOwner.Visible = false;
                            this.imgAPContributer.Visible = true;
                            //this.ibtnSubmit.Text = "Close";
                            //this.ibtnSubmit.ToolTip = "CloseTips";
                            if (!((APAccessControl)this.Access).IsReadOnly)
                            {
                                if (!evService.IsCodStaffForAP(HttpContext.Current.User.Identity.Name, this.MeetingID))
                                {
                                    this.ibtnSubmit.Visible = false;

                                }
                                else
                                {
                                    this.ibtnSubmit.Visible = true;
                                    this.IsReadOnly = false;
                                }
                            }
                            else
                            {
                                if (evService.IsCodStaffForAP(HttpContext.Current.User.Identity.Name, this.MeetingID))
                                {
                                    if (this.ActionPointRecord.Status == "Close")
                                    {
                                        this.ibt_Modify.Visible = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            this.IsReadOnly = false;
                            //this.ibtnSave.Visible = false;
                            //this.imgAddAPComment.Visible = false;
                            this.imgAddAPComment.Visible = true;
                        }

                        //AP is end
                        if (APInfo.Rows[0]["APStep"].ToString() == WorkTransition.AP_End)
                        {
                            this.apAttachment1.SetAttachVisible(false);
                            this.ibtnSave.Visible = false;
                            this.ibtnSubmit.Visible = false;
                            this.imgAPContributer.Visible = false;
                            this.imgAPOwner.Visible = false;
                            this.imgAddAPComment.Visible = false;
                        }

                        #endregion

                    }
                }
                else
                {
                    //AP has been requested, but not submitted yet
                    IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                    bool isAllowed = queryService.isAllowAPStart(this.MeetingID);
                    if (!isAllowed)
                    {
                        this.ibtnSubmit.Visible = false;
                        if (!queryService.isAPEditable(this.MeetingID))
                        {
                            DisableActionPoint();
                        }

                    }
                    this.APTrace.Visible = false;
                    this.attachment.Visible = false;
                }

                #endregion

                #region Set AP Information

                if (ActionPointRecord != null)
                {
                    InitCommentHistory();
                    if (this.IsReadOnly || this.Access.IsReadOnly)
                    {
                        #region display labels
                        this.txtNo.Visible = false;
                        this.ddlCAT.Visible = false;
                        this.txtStatus.Visible = false;
                        this.ddlClassfication.Visible = false;
                        this.ddlPRT.Visible = false;
                        this.ddlType.Visible = false;
                        this.txtDescription.Visible = false;
                        this.txtDueTime.Visible = false;

                        this.txtClosedDate.Visible = false;
                        this.spanClosedDate.Visible = false;

                        this.txtOperator.Visible = false;
                        this.spanDueDate.Visible = false;

                        this.imgAPOwner.Visible = false;
                        this.imgAPContributer.Visible = false;
                        if (Access.IsReadOnly)
                        {
                            this.imgAddAPComment.Visible = false;
                            this.apAttachment1.SetAttachVisible(false);

                            this.ibtnSubmit.Visible = false;
                            this.ibtnSave.Visible = false;
                        }

                        this.lblAPNoValue.Text = ActionPointRecord.APNo;
                        this.lblCATValue.Text = ActionPointRecord.CAT;
                        this.lblStatusValue.Text = ActionPointRecord.Status;
                        this.lblClassValue.Text = ActionPointRecord.Classifictaion;
                        this.lblPRTValue.Text = ActionPointRecord.Priority;
                        this.lblTypeValue.Text = ActionPointRecord.APType;

                        this.Description.InnerHtml = ActionPointRecord.Description;
                        if (ActionPointRecord.DueTime != Nullables.NullableDateTime.Default)
                        {
                            this.txtDueTime.Text = ActionPointRecord.DueTime.Value.ToString("MM/dd/yyyy");
                            this.lblDueTimeValue.Text = this.txtDueTime.Text;
                        }
                        if (ActionPointRecord.ClosedDate != Nullables.NullableDateTime.Default)
                        {
                            this.txtClosedDate.Text = ActionPointRecord.ClosedDate.Value.ToString("MM/dd/yyyy");
                            this.lbClosedDate.Text = this.txtClosedDate.Text;
                        }
                        this.lblOperatorValue.Text = ActionPointRecord.OperatorName;

                        this.lblAPNoValue.Visible = true;
                        this.lblCATValue.Visible = true;
                        this.lblClassValue.Visible = true;
                        this.lblPRTValue.Visible = true;
                        this.lblTypeValue.Visible = true;
                        this.lblDueTimeValue.Visible = true;
                        this.lbClosedDate.Visible = true;
                        this.lblOperatorValue.Visible = true;
                        this.lblStatusValue.Visible = true;

                        //Hide datagrid action
                        this.dtGridAPContributers.Columns[4].Visible = false;
                        this.dtGridAPOwner.Columns[4].Visible = false;

                        #endregion

                    }
                    else
                    {
                        this.txtNo.Text = ActionPointRecord.APNo;
                        this.InitDropDownList();

                        this.ddlCAT.SelectedValue = ActionPointRecord.CAT;
                        this.ddlClassfication.SelectedValue = ActionPointRecord.Classifictaion;
                        this.ddlPRT.SelectedValue = ActionPointRecord.Priority;
                        this.ddlType.SelectedValue = ActionPointRecord.APType;
                        this.txtDescription.Text = ActionPointRecord.Description;
                        if (ActionPointRecord.DueTime != Nullables.NullableDateTime.Default)
                        {
                            this.txtDueTime.Text = ActionPointRecord.DueTime.Value.ToString("MM/dd/yyyy");
                            this.lblDueTimeValue.Text = this.txtDueTime.Text;
                        }
                        if (ActionPointRecord.ClosedDate != Nullables.NullableDateTime.Default)
                        {
                            this.txtClosedDate.Text = ActionPointRecord.ClosedDate.Value.ToString("MM/dd/yyyy");
                            this.lbClosedDate.Text = this.txtClosedDate.Text;
                        }
                        this.txtOperator.Text = ActionPointRecord.OperatorName;

                        this.txtStatus.Text = ActionPointRecord.Status;

                    }
                }
                #endregion
            }
        }

        private void DisableActionPoint()
        {
            this.ibtnSave.Visible = false;
            this.IsReadOnly = true;
        }

        private void BuildPartner2Save(ref IList ap_partnerList)
        {
            APPartner partner = null;
            IUtility utilityService = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            DataTable table = (DataTable)this.APOwnerContributer;
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["APType"].ToString() == "Contributer")
                    {
                        this.IsHasOwner = true;
                    }
                    partner = new APPartner();
                    partner.Employee = new Employee();
                    partner.Employee.Id = row["EmployeeNO"].ToString();
                    partner.PartnerType = utilityService.GetDicItem(row["APType"].ToString(), "APPartnerType");

                    ap_partnerList.Add(partner);
                }
            }
        }

        private void BindObj2Save()
        {
            if (this.ActionPointRecord == null)
                this.ActionPointRecord = new Entity.ActionPoint();

            ActionPointRecord.APNo = this.txtNo.Text;
            ActionPointRecord.Status = this.txtStatus.Text;
            ActionPointRecord.Priority = this.ddlPRT.SelectedValue;
            ActionPointRecord.CAT = this.ddlCAT.SelectedValue;
            ActionPointRecord.Classifictaion = this.ddlClassfication.SelectedValue;
            ActionPointRecord.APType = this.ddlType.SelectedValue;
            if (this.txtDueTime.Text != string.Empty)
                ActionPointRecord.DueTime = new Nullables.NullableDateTime(Convert.ToDateTime(txtDueTime.Text));
            if (this.txtClosedDate.Text != string.Empty)
                ActionPointRecord.ClosedDate = new Nullables.NullableDateTime(Convert.ToDateTime(this.txtClosedDate.Text));
            ActionPointRecord.OperatorName = this.txtOperator.Text;
            ActionPointRecord.Description = this.txtDescription.Text;

            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
            ActionPointRecord.EVMeeting = evService.GetEVMeetingByID(this.MeetingID);

            this.imgAddAPComment.Visible = true;
        }
        void ibtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                if (!this.IsReadOnly)
                {
                    this.BindObj2Save();
                    IList partnerList = new ArrayList();
                    this.BuildPartner2Save(ref partnerList);
                    if (this.IsHasOwner)
                    {
                        evService.SaveAPRequest(this.ActionPointRecord, partnerList);

                        //this.CloseAP();

                        //Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='refresh';window.close();</script>");
                        Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='refresh';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Please select AP Contributers!');</script>");
                    }
                }
                else
                {
                    if (Request.QueryString["State"] == "Query" && this.ibtnSave.Visible)
                    {
                        IList partnerList = new ArrayList();
                        this.BuildPartner2Save(ref partnerList);
                        if (partnerList.Count <= 0)
                        {
                            Response.Write("<script>alert('Please select AP Contributers!');</script>");
                            return;
                        }
                        else
                        {
                            evService.SaveAPRequest(this.ActionPointRecord, partnerList);
                        }
                    }
                    else
                    {
                        evService.SaveAPRequest(this.ActionPointRecord, null);
                    }
                    //Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='refresh';window.close();</script>");
                    Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='refresh';</script>");
                }


                ////Automatic Close the Action Point: True - Close AP
                //if (evService.IsAutomaticCloseAP(this.ActionPointRecord))
                //{
                //    evService.SubmitAPRequest(this.ActionPointRecord, null);
                //}
            }
            catch (GDCException gdcEx)
            {
                throw gdcEx;
            }
            finally
            {
            }
        }


        private void CloseAP()
        {
            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;

            //Automatic Close the Action Point: True - Close AP
            if (evService.IsAutomaticCloseAP(this.ActionPointRecord))
            {
                evService.SubmitAPRequest(this.ActionPointRecord, null);
            }
        }

        void ibtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                if (!this.IsReadOnly)
                {
                    this.BindObj2Save();
                    if (string.IsNullOrEmpty(this.txtClosedDate.Text))
                    {
                        ActionPointRecord.ClosedDate = DateTime.Now;
                    }
                    
                    IList partnerList = new ArrayList();
                    this.BuildPartner2Save(ref partnerList);
                    if (this.IsHasOwner)
                    {
                        evService.SubmitAPRequest(this.ActionPointRecord, partnerList);
                        Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Please select AP Contributers!');</script>");
                    }
                }
                else
                {
                    evService.SubmitAPRequest(this.ActionPointRecord, null);
                    Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
                }
            }
            catch (GDCException gdcEx)
            {
                throw gdcEx;
            }
            finally
            {
            }
        }

        private void ibtnQuery_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                evService.RejectAP2Owner(this.ActionPointRecord, null);
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch (GDCException gdcEx)
            {
                throw gdcEx;
            }
            finally
            {
            }
        }


        protected void dtGridAPOwner_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string itemId = e.Item.Cells[0].Text;
                DataTable table = (DataTable)this.APOwnerContributer;
                if (table != null)
                {
                    table.Rows.Remove(table.Rows[Convert.ToInt32(itemId) - 1]);

                    this.dtGridAPOwner.DataSource = this.SetRowID(table);
                    this.dtGridAPOwner.DataBind();
                    this.APOwnerContributer = table;
                    InitOwnerContributer();
                }
            }
        }

        private void dtGridAPOwner_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton btnDel = e.Item.Cells[4].Controls[0] as LinkButton;
                if (btnDel != null)
                {
                    btnDel.Attributes["onclick"] = "return confirm('Are you sure to delete this item?')";
                }
            }
        }

        private void dtGridAPContributers_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton btnDel = e.Item.Cells[4].Controls[0] as LinkButton;
                if (btnDel != null)
                {
                    btnDel.Attributes["onclick"] = "return confirm('Are you sure to delete this item?')";
                }
            }
        }

        private void dtGridAPContributers_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string itemId = e.Item.Cells[0].Text;
                DataTable table = (DataTable)this.APOwnerContributer;
                if (table != null)
                {
                    table.Rows.Remove(table.Rows[Convert.ToInt32(itemId) - 1]);

                    this.dtGridAPContributers.DataSource = this.SetRowID(table);
                    this.dtGridAPContributers.DataBind();
                    this.APOwnerContributer = table;
                    InitOwnerContributer();
                }
            }
        }

        void ibt_Modify_Click(object sender, EventArgs e)
        {
            if (this.ibt_Modify.Text == "Modify")
            {
                this.lbClosedDate.Visible = false;
                this.txtClosedDate.Visible = true;
                this.spanClosedDate.Visible = true;

                this.lblDueTimeValue.Visible = false;
                this.txtDueTime.Visible = true;
                this.spanDueDate.Visible = true;
                this.ibt_Modify.Text = "Save";
                this.ibt_Modify.ToolTip = "SaveToolTips";

                this.txtClosedDate.Text = this.lbClosedDate.Text;
                this.txtDueTime.Text = this.lblDueTimeValue.Text;

            }
            else
            {
                try
                {
                    Nullables.NullableDateTime cd = Nullables.NullableDateTime.Default;
                    Nullables.NullableDateTime dd = Nullables.NullableDateTime.Default;

                    if (this.txtClosedDate.Text != string.Empty)
                        cd = new Nullables.NullableDateTime(Convert.ToDateTime(this.txtClosedDate.Text));

                    if (this.txtDueTime.Text != string.Empty)
                        dd = new Nullables.NullableDateTime(Convert.ToDateTime(this.txtDueTime.Text));

                    IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                    evService.UpdateCloseDate(this.APID, cd, dd);

                    this.lblDueTimeValue.Text = this.txtDueTime.Text;
                    this.lblDueTimeValue.Visible = true;
                    this.txtDueTime.Visible = false;
                    this.spanDueDate.Visible = false;

                    this.lbClosedDate.Text = this.txtClosedDate.Text;
                    this.lbClosedDate.Visible = true;
                    this.txtClosedDate.Visible = false;
                    this.spanClosedDate.Visible = false;
                    this.ibt_Modify.Text = "Modify";
                    this.ibt_Modify.ToolTip = "ModifyTips";
                }
                catch
                {

                }

            }
        }

        #region IPageAccess Members

        public AccessControl Access
        {
            get
            {
                return this.apAccess;
            }
            set
            {
                this.apAccess = value;
            }
        }

        #endregion
    }

    public class APAccessControl : UserControls.AccessControl
    {

        private int apId;
        private string status;

        public int APID
        {
            get
            {
                return this.apId;
            }
            set
            {
                this.apId = value;
            }
        }


        public override string State
        {
            set
            {
                this.status = value;
            }
        }

        public APAccessControl(int apId)
        {
            this.apId = apId;
        }
        public override bool IsReadOnly
        {
            get
            {
                if (status == "Readonly")
                    return true;
                else
                {
                    IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                    DataTable APInfo = evService.GetAPInfo(APID);
                    if (APInfo != null && APInfo.Rows.Count > 0)
                    {
                        if (APInfo.Rows[0]["APStep"].ToString() == WorkTransition.AP_End)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                        return false;
                }

            }
        }

    }
}
